package com.gem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.entity.Order;
import com.gem.entity.OrderItem;
import com.gem.entity.Product;
import com.gem.entity.User;
import com.gem.service.*;
import com.gem.vo.ItemVO;
import com.gem.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 刘聪
 * Created  on 2021/4/21
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService itemService;
    @Autowired
    private UserAddressService addressService;
    @Autowired
    private CartService cartService;
    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    @ResponseBody
    public String orderPayType(@RequestBody List<Map> mapList, HttpSession session) {
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        QueryWrapper wrapper = new QueryWrapper();
        Double total = 0.00;
        for (Map<String, Object> stringObjectMap : mapList) {
            Integer productId = (Integer) stringObjectMap.get("productId");
            Integer quantity = (Integer) stringObjectMap.get("quantity");
//            Double cost = (Double) stringObjectMap.get("cost");

            Product product = productService.getById(productId);
            Double price = product.getPrice();
            OrderItem item = new OrderItem();
            Double totalPrice = price * quantity;
            total += totalPrice;
            item.setUserId(6);
            item.setOrderNo(uuid);
            item.setProductId(productId);
            item.setPrice(price);
            item.setQuantity(quantity);
            item.setProductName(product.getName());
            item.setSubImages(product.getSubImages());
            item.setTotalPrice(totalPrice);
            itemService.save(item);

        }

       /* for(int i=0; i<jsonArray.size(); i++){
            @RequestBody String json,
            JSONArray jsonArray = JSONArray.fromObject(obj);
            System.out.println(jsonArray.get(i));
            Object object = jsonArray.get(i);
            JSONObject jsonObject = JSONObject.fromObject(object);
//            HPolicyTypeDictDto hPolicyTypeDictDto =(HPolicyTypeDictDto) JSONObject.toBean(jsonObject, HPolicyTypeDictDto.class);
        }*/
        User user = (User) session.getAttribute("User");

        Order order = new Order();
        order.setUserId(user.getId());
        order.setOrderNo(uuid);
        order.setTotalPrice(total);
        order.setStatus(10);
        order.setPostage(0);
        order.setShippingId((Integer) mapList.get(0).get("addressId"));
        orderService.save(order);
        wrapper.eq("user_id", user.getId());
        cartService.remove(wrapper);
        return "success";
    }

    @GetMapping("/orderList")
    public String orderList(HttpSession session, Model model) {
        User user = (User) session.getAttribute("User");

        List<OrderVO> voList = orderService.orderList(user.getId());
        model.addAttribute("orderList", voList);
//        model.addAttribute("tabTwoList",)
        return "order/orderList";
    }

    @GetMapping("/orderinfo/{orderNo}/{shippingId}")
    public String orderInfo(@PathVariable String orderNo,
                            @PathVariable Integer shippingId,
                            Model model) {
        ItemVO itemVO = itemService.itemList(orderNo, shippingId);
        model.addAttribute("itemList", itemVO);
        return "order/orderinfo";
    }

    @GetMapping("/cancel/{orderNo}")
    public String cancelOrder(@PathVariable String orderNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_no", orderNo);
        Order one = new Order();
        one.setStatus(0);
        orderService.update(one, wrapper);
        return "redirect:/order/orderList";
    }

    @GetMapping("/delete/{orderNo}")
    public String deleteOrder(@PathVariable String orderNo) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("order_no", orderNo);
        orderService.remove(wrapper);
        itemService.remove(wrapper);
        return "redirect:/order/orderList";
    }
}
